/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INCLUDE_DRV_IRQ_H
#define INCLUDE_DRV_IRQ_H

#include "Platform_Types.h"
#include <Compiler.h>

#include "arch_irq.h"

/* trigger mode */
#define TRIGGER_MODE_LEVEL 0
#define TRIGGER_MODE_EDGE  1
/* irq default priority */
#define IRQ_DEFAULT_PRIORITY                       (14)

#if !defined(ASSEMBLY)


/* irq callback type */
#if defined(NON_AUTOSAR_STANDARD_IRQ_HANDLER)
typedef int (*irq_handler)(uint32 irq, void *arg);
#else
typedef void (*irq_handler)(void);
#endif
/*
 * irq initialize.
 *
 * @base   irq base
 * @intr_num  irq number
 */
int irq_initialize(uint32 base, uint32 intr_num);
/**
 * @brief Initialize interrupt controller secondary.
 *
 *  This function is used to initialize gic except for main core
 *
 * @param [in] base base address.
 * @param [in] intr_num number of interrupts.
 * @return SDRV_GIC_STATUS_SUCCESS : initialized success.
 *         SDRV_GIC_STATUS_INVALID_PARAM : invalid param.
 */
int irq_initialize_secondary(uint32 base, uint32 intr_num);

/*
 * irq attach.
 *
 * @irq     irq id.
 * @handler irq callback handler.
 * @arg     irq callback arg.
 * @return  result.
 */
int irq_attach(uint32 irq, irq_handler handler, void *arg);

/*
 * irq detach.
 *
 * @irq     irq id.
 * @return  result.
 */
int irq_detach(uint32 irq);

/*
 * interrupt dispatch.
 *
 * @return  irq dispatch result.
 */
int irq_dispatch(void);

/*
 * irq enable.
 *
 * @irq   irq number.
 */
int irq_enable(uint32 irq);

/*
 * irq disable.
 *
 * @irq   irq number.
 */
int irq_disable(uint32 irq);

/*
 * interrupt set priority mask.
 *
 * @mask     int priority mask.
 * @return   old int priority mask.
 */
uint32 irq_mask(uint32 mask);

/*
 * interrupt unmask all int.
 */
void irq_unmask(void);

/*
 * set irq priority.
 *
 * @irq         irq number.
 * @priority    irq priority.
 */
int irq_set_priority(uint32 irq, uint32 priority);

/*
 * get irq priority.
 *
 * @irq         irq number.
 * @return      irq priority.
 */
int irq_get_priority(uint32 irq);

/*
 * interrupt get current int priority.
 *
 * @return      int priority.
 */
uint32 irq_get_current_priority(void);

/**
 * @brief raise sgi.
 *
 * one sgi interrupt can be raised to multi-core.
 *
 * @param [in] sgi_id sgi number.
 * @param [in] target_list bitmask of the multi-target.
 * @return SDRV_GIC_STATUS_SUCCESS : raise sgi success.
 *         SDRV_GIC_STATUS_INVALID_PARAM : invalid param.
 */
uint32 irq_raise_sgi(unsigned int sgi_id, uint16 target_list);

/*
 * irq save.
 *
 * @return  old irq state.
 */
#define IRQ_SAVE                                                                                                       \
    irq_state_t irq_flags;                                                                                             \
    irq_flags = arch_irq_save();

/*
 * irq restore.
 *
 * @flags   old irq state.
 */
#define IRQ_RESTORE arch_irq_restore(irq_flags);


#endif

#endif
